package cn.edu.sdjzu.xg.eagle.service.impl;

import cn.edu.sdjzu.xg.eagle.dao.StudentDao;
import cn.edu.sdjzu.xg.eagle.entity.ActorType;
import cn.edu.sdjzu.xg.eagle.entity.Student;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BaseException;
import cn.edu.sdjzu.xg.eagle.factory.DaoFactory;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.service.StudentService;
import cn.edu.sdjzu.xg.eagle.service.UserService;
import cn.edu.sdjzu.xg.eagle.util.Condition;
import cn.edu.sdjzu.xg.eagle.util.IdUtil;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;

public class StudentServiceImpl implements StudentService {
    static StudentDao studentDao = null;
    static UserService userService = null;

    static {
        try {
            studentDao = DaoFactory.getDao(StudentDao.class, "StudentDaoImpl");
            userService = ServiceFactory.getService(UserService.class, "UserServiceImpl");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public Student find(Long id) throws SQLException, BaseException, InvocationTargetException {
        Connection connection = null;
        try {
            connection = JdbcHelper.getConn();

            Student student = studentDao.find((Condition) connection, id);
            return student;
        } finally {
            JdbcHelper.close(connection);
        }
    }

    public boolean addStudent(Student student) throws SQLException, BaseException, InvocationTargetException {
        Connection connection = null;
        try {
            connection = JdbcHelper.getConn();
            connection.setAutoCommit(false);

            Long studentId = IdUtil.getLongId();
            student.setId(studentId);
            User user = new User();
            user.setUsername(student.getNo());
            user.setPassword(student.getNo());
            user.setActorType(ActorType.STUDENT);
            user.setActor(student);
            Integer userId = userService.add(connection, user);
            if (userId == null) {
                throw new BaseException("添加用户失败.");
            }
            user.setId(userId);
            student.setUser(user);
            boolean isAdded = studentDao.addStudent((Condition) connection, student);
            if (!isAdded) {
                throw new BaseException("添加学生失败.");
            }

            connection.commit();
            return true;
        } catch (SQLException | BaseException | InvocationTargetException e) {
            if (connection != null) {
                connection.rollback();
            }
            e.printStackTrace();
            throw new BaseException("数据库操作异常.");
        } finally {
            JdbcHelper.close(connection);
        }
    }

    @Override
    public boolean delete(Long id) throws SQLException, BaseException, InvocationTargetException {
        Connection connection = null;
        try{
            if (id == null||id<= 0){
                throw new BaseException("无效的学生id:"+id);
            }
            connection = JdbcHelper.getConn();
            connection.setAutoCommit(false);
            boolean isUserDeleted = userService.delete(connection,id);
            if (!isUserDeleted) {
                connection.rollback();
                throw new BaseException("删除关联用户失败");
            }
            int studentRows = studentDao.delete(connection,id);
            if (studentRows > 0) {
                connection.rollback();
                throw new BaseException("未找到学生记录,ID"+id);
            }
            connection.commit();
            return true;
        } catch (SQLException e) {
            if(connection != null&&connection.isClosed()){
                connection.rollback();
            }
            throw new SQLException("数据库操作异常:"+e.getMessage(),e);
        }catch (BaseException e){
            throw e;
        }catch (Exception e){
            throw new InvocationTargetException(e,"数据库操作失败");
        }finally {
            JdbcHelper.close(connection);
        }
    }
}
